#!/bin/sh /etc/rc.common
# Copyright (C) 2018 wongsyrone

. /lib/functions.sh

START=95
USE_PROCD=1
#PROCD_DEBUG=1

PROG=/usr/sbin/trojan
CONF=/etc/trojan.json

config_load "trojan"

parse_trojan() {
	config_get ENABLED "$section" "enabled"
}

config_foreach parse_trojan 'trojan'


start_service() {
	if [ "1" = "$ENABLED" ] || [ "on" = "$ENABLED" ] || [ "true" = "$ENABLED" ]; then
		procd_open_instance
		procd_set_param command $PROG --config $CONF
		procd_set_param user root # run service as user root
		procd_set_param stdout 1 # forward stdout of the command to logd
		procd_set_param stderr 1 # same for stderr
		procd_set_param limits nofile="1048576 1048576" # max allowed value can be fetched via /proc/sys/fs/nr_open
		[ -e /proc/sys/kernel/core_pattern ] && {
			procd_append_param limits core="unlimited"
		}
		procd_close_instance
	else
		echo "trojan is disabled"
	fi
}
